package com.mic.yzzn.plc.common.instructions.request;

import com.mic.yzzn.plc.common.ins.datastructure.PLCContactorNumber;
import com.mic.yzzn.plc.common.instructions.basic.PLCRequestInstruction;
import com.mic.yzzn.plc.common.toolUtils.PLCContactorCode;
import com.mic.yzzn.plc.common.toolUtils.PLCDataCode;
import org.apache.commons.lang.StringUtils;

/**
 * Created by ASH on 16/3/7.
 */
public class PLCSingleContactorWriteInstruction extends PLCSingleContactorInstruction
{
	/** 触点数据 */
    protected String contactorData;

    /**
     * 初始化方法
     */
    public PLCSingleContactorWriteInstruction()
    {
        new PLCSingleContactorInstruction();

        requestCode = PLCRequestInstruction.WRITE_SINGLE_CONTACTOR_INS;
        contactorData = PLCDataCode.CONTACTOR_STATE_OFF;
    }

    /**
     * 触点数据get方法
     * @return 触点数据
     */
    public String getContactorData()
    {
        return contactorData;
    }

    public void setContactorData(String contactorData)
    {
        this.contactorData = contactorData;
    }

    /**
     * 设置触点数据为OFF
     */
    public void setContactorOff()
    {
        contactorData = PLCDataCode.CONTACTOR_STATE_OFF;
    }

    /**
     * 设置触点数据为ON
     */
    public void setContactorOn()
    {
        contactorData = PLCDataCode.CONTACTOR_STATE_ON;
    }

    /**
     * 转换成字符串
     */
    @Override
    public String toString()
    {
        // 拼接指令字符串
        StringBuffer buffer = new StringBuffer();
        buffer.append(instructionHeader);
        buffer.append(locationNumber_high);
        buffer.append(locationNumber_low);
        buffer.append(instructionTypeCode);
        buffer.append(requestCode);
        buffer.append(contactorCode);
        buffer.append(contactorNumber.toString());
        buffer.append(contactorData);
        buffer.append(getBBC_high());
        buffer.append(getBBC_low());
        buffer.append(instructionEnd);

        return buffer.toString();
    }

    /**
     * 计算并且按位异或的值
     */
    @Override
    protected int getXORByBit()
    {
        int i = 0;
        int result = 0;
        for (i = 0; i < contactorData.length(); ++i)
        {
            result ^= contactorData.charAt(i);
        }

        return super.getXORByBit() ^ result;
    }
    
    @Override
    public String initWithString(String instructionString) throws IllegalArgumentException
    {
    	String tempResult = super.initWithString(instructionString);
    	
//    	System.out.println(tempResult);
    	
    	// 判断请求码与当前指令的请求码是否符合
    	int requestCodeLength = requestCode.length();
    	if (!(StringUtils.substring(tempResult, 0, requestCodeLength).equals(requestCode)))
		{
			throw new IllegalArgumentException("PLCSingleContactorReadInstruction's requestCode must be \"" + requestCode +"\"");
		}
    	
    	// 截取请求码
    	tempResult = StringUtils.substringAfter(tempResult, requestCode);
    	
    	// 获得触点代码
    	String contactorCode = tempResult.substring(0, 1);
    	
    	this.contactorCode = contactorCode;
    	
    	// 截断单触点代码
    	tempResult = StringUtils.substringAfter(tempResult, contactorCode);
//    	System.out.println(tempResult);
    	
    	int leftLength = tempResult.length();
    	
    	// 获取触点编号
    	PLCContactorNumber contactorNumber = new PLCContactorNumber(tempResult.substring(0, leftLength - 2), 
    			tempResult.charAt(leftLength - 2));
    	
    	// 设置触点编号
    	this.contactorNumber = contactorNumber;
    	
    	// 截断触点编号
    	tempResult = StringUtils.substringAfter(tempResult, contactorNumber.toString());
//    	System.out.println(tempResult);

    	// 获取触点数据
    	String contactorData = tempResult;
    	
    	// 判断触点数据合法性
    	if (!contactorData.equals(PLCContactorCode.PLC_CONTACTOR_OFF) &&
    			!contactorData.equals(PLCContactorCode.PLC_CONTACTOR_ON))
		{
			throw new IllegalArgumentException("contactor data must be \"1\" or \"0\"");
		}
    	
    	this.contactorData = contactorData;
    	
    	return tempResult;
    }
}
